package com.hxkj.controller;

import com.hxkj.config.SftpConfig;
import com.hxkj.domain.po.Resources;
import com.hxkj.domain.po.ResourcesTree;
import com.hxkj.service.ResourcesService;
import com.integral.mall.file.storage.sftp.SFTPStorage;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 资源控制层
 * <p>
 * Created by yaosy on 2018年6月11日
 */
@RestController
@RequestMapping("/resources")
public class ResourcesController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResourcesController.class);

    @Autowired
    private ResourcesService resourcesService;

    @Resource
    private SftpConfig sftpConfig;

    /**
     * 新建文件资源
     *
     * @param resource
     * @throws Exception
     */
    @RequestMapping(value = "/saveResources", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultDataStruct<Resources>> saveResources(@RequestBody Resources resource) {
        return ()-> resourcesService.saveResources(resource);
    }

    /**
     * 新建目录资源
     *
     * @param resource
     * @throws Exception
     */
    @RequestMapping(value = "/saveResourcesFolder", method = RequestMethod.POST,consumes  ="application/json;charset=UTF-8")
    public Callable<ResultDataStruct<Resources>> saveResourcesFolder(@RequestBody Resources resource) throws Exception {
        SFTPStorage sftpStorage =  sftpConfig.sftpStorage(sftpConfig.storageFactory());
        return ()-> resourcesService.saveResourcesFolder(resource, sftpStorage);
    }

    /**
     * 资源功能树
     *
     * @param resource
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getResourcesTree",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultDataStruct<List<ResourcesTree>>>  getResourcesTree(@RequestBody Resources resource) throws Exception {
        return ()-> resourcesService.getResourcesTree(resource);
    }


    /**
     * 删除资源
     *
     *
     * @param resource
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/remove",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultMessageStruct>  remove(@RequestBody Resources resource) {
        return ()-> resourcesService.remove(resource);
    }

    /**
     * 获取单条模板数据
     *
     * @param resource
     * @throws Exception
     */
    @RequestMapping(value = "/get",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultDataStruct<Resources>> get(@RequestBody Resources resource) {
        return ()-> resourcesService.get(resource);
    }

    /**
     * 保存单条资源
     * @param resource
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/modify",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultMessageStruct>  modify(@RequestBody Resources resource)  throws Exception{
        SFTPStorage sftpStorage =  sftpConfig.sftpStorage(sftpConfig.storageFactory());
        return ()-> resourcesService.modify(resource,sftpStorage);
    }


    /**
     * 模板上传后需要保存的信息
     *
     * @param resource
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveUploadContent",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultDataStruct<Resources>> saveUploadContent(@RequestBody Resources resource) {
        return ()-> resourcesService.saveUploadContent(resource);
    }


    /**
     * 根据站点获取所有的模板文件
     *
     * @param resource
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getResourcesBySiteId",consumes  ="application/json;charset=UTF-8",method = RequestMethod.POST)
    public  Callable<ResultDataStruct<List<Resources>>> getResourcesBySiteId(@RequestBody Resources resource) {
        return ()-> resourcesService.getResourcesBySiteId(resource);
    }

    /**
     * @author Mr.zheng
     * @Description modifyResourcesMd5 修改添加资源文件的MD5值
     * @Date 10:06 2018/8/20
     * @Param [resources]
     * @return com.platform.common.domain.result.ResultDataStruct
     **/
    @RequestMapping(value = "/modifyResourcesMd5", method = RequestMethod.POST,consumes = "application/json;charset=UTF-8")
    public Callable<ResultMessageStruct> modifyResourcesMd5(@RequestBody Resources resources) throws Exception{
        return ()-> resourcesService.modifyResourcesMd5(resources);
    }

    /**
     * 匹配文件路径
     * @return com.platform.common.domain.result.ResultDataStruct
     **/
    @RequestMapping(value = "/selectContainPathFolder", method = RequestMethod.POST,consumes = "application/json;charset=UTF-8")
    public Callable<List<Resources>> selectContainPathFolder(@RequestBody Resources resources) throws Exception{
        return ()-> resourcesService.selectContainPathFolder(resources);
    }

}
